#include <interface\weCtr.h>
#include <interface\gl.h>

weCtr::weCtr(){}
weCtr::~weCtr(){

}

void            weCtr::addToElmts(winElmt* el){
    elements.push_back(el);
}
void            weCtr::remFromElmts(winElmt* el,bool suppr){
    vector<winElmt*>::iterator i;
    for( i = elements.begin(); i < elements.end() && (*i) != el; i++ ){}
    if( i != elements.end() ){
        if( suppr ) delete (*i);
        elements.erase(i);
    }
}
unsigned char weCtr::elEventExec(unsigned char status){
    if( !elements.size() ) return status;
    glDisable(GL_DEPTH_TEST);
    unsigned char oldstatus = status;
    for( vector<winElmt*>::reverse_iterator i = elements.rbegin(); i < elements.rend(); i++ ){
        status = (*i)->eventExec(oldstatus);
        if( (status & ACTIVE_TAKEN) && !(status & NOT_ACTIVABLE ) && !(oldstatus & ACTIVE_TAKEN) && (i != elements.rbegin())){
                    winElmt* stack = (*i);
                    vector<winElmt*>::iterator k = i.base();
                    k--;
                    elements.erase(k);
                    elements.push_back(stack);
        }
        oldstatus = status;
    }
    if( status & ACTIVE_TAKEN && status & NOT_ACTIVABLE ){
        for( vector<winElmt*>::iterator i = elements.begin(); i < elements.end(); i++ ){
            (*i)->activeLoseEvent();
        }
    }
    return status;
}
void            weCtr::elDraw(){
    for( vector<winElmt*>::iterator i = elements.begin(); i < elements.end(); i++ ){
        (*i)->display();
        (*i)->display_end();
    }
}
